load libraries

library("tidyverse")
library("plyr")
library("dplyr")
library("ggplot2")
library("RColorBrewer")
library("data.table")
library("stringr")
library("janitor")
library("knitr")
library("kableExtra")
library("plotly") 

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
                 #    fig.width = 20,
                  #    fig.asp = 0.6,
                   #   out.width = "100%")

load data

data <- read.csv("/Users/nuriteliash/Documents/GitHub/varroa_Ploidy/data/Ploidy.csv") %>%
  dplyr::mutate(Family = as.character(Family)) %>%
  dplyr::mutate(stage_mature = case_when(
    grepl("adult", Stage) ~ "Mature",
    !grepl("adult", Stage) ~ "Imature"))

# order the levels 
data$body.part <- factor(data$body.part, level=c("Body", "Anterior", "Posterior", "Legs","Hemolymph","Ovary","Testes"))

data$Stage <- factor(data$Stage, level=c("Larvae", "Protonymph", "Deuteronymph", "adult"))

data$Stage_original <- factor(data$Stage_original, level=c("Mom", "Son", "Mature","Daughter", "Deuteronymph", "Protonymph", "Larvae", "Immature"))

levels(data$Family) <- c(levels(data$Family), 0)
data$Family <- factor(data$Family, level= c("0", "1", "11","27","3","2","4", "5"))

p_family_adults = data %>% dplyr::filter(body.part %in% c("Body", "Ovary","Testes")) %>%
   dplyr::filter(Stage == "adult") %>%
   mutate_at("Family", ~replace_na(.,"0")) %>%
  ggplot(aes(y=Ploidy, x=Sex, fill = Sex, lable = Stage)) + 
 geom_boxplot(outlier.shape = NA, coef=0 ) +  theme_classic() +  geom_jitter(width=0.1, size=2) +
  facet_wrap(~body.part ) + 
      theme(axis.title.x = element_blank(),
            axis.text.x = element_blank(),
            axis.ticks = element_blank(),
    panel.border=element_rect(colour="black",size=1, fill = NA))+
ggtitle('Mite ploidy in differnet body parts') +  ylim(0, 3)

p_fam_body = data %>% dplyr::filter(body.part == "Body") %>%
 dplyr::filter(Stage %in% c("Larvae", "Protonymph", "Deuteronymph", "adult")) %>%
  ggplot(aes(y=Ploidy, x=Sex, fill = Sex, lable = Stage)) + 
 geom_boxplot(outlier.shape = NA, coef=0 ) +  theme_classic() +  geom_jitter(width=0.1, size=2) +
facet_wrap(~Stage, nrow = 1 ) + 
  ggtitle('Mite Ploidy in whole body, in differnet developmental stage') +
theme(axis.title.x = element_blank(),
            axis.text.x = element_blank(),
            axis.ticks = element_blank(),
    panel.border=element_rect(colour="black",size=1, fill = NA))+
  theme(legend.position='right')+  ylim(0, 3)

plot histograms

plot by family

p_allFam_body_adult = data %>% dplyr::filter(body.part == "Body") %>%
 dplyr::filter(Stage =="adult") %>%
 #   mutate_at("Family", ~replace_na(.,"0")) %>%
  na.omit() %>%
  ggplot(aes(y=Ploidy, x=Family, fill = Sex, lable = Stage)) + 
 geom_boxplot(outlier.shape = NA, coef=0 ) +  theme_bw() +  geom_jitter(width=0.1, size=2) +
#facet_wrap(~Stage ) + 
  ggtitle('Mite Ploidy in whole body, per family') +
   theme(axis.text.x = element_text(angle = 45)) +  theme(legend.position='right')+  ylim(0, 3)

p_fam_body_1_11_27_3 = data %>% dplyr::filter(body.part == "Body") %>%
  dplyr::filter(Family %in% c("1", "11","27","3")) %>%
  ggplot(aes(y=Ploidy, x=Stage_original, fill = Sex, lable = Stage_original)) + 
 geom_boxplot(outlier.shape = NA, coef=0 ) +  theme_bw() +  geom_jitter(width=0.1, size=2) +
  facet_wrap(~Family , nrow = 1) + ggtitle('Mite Ploidy per family') +
   theme(axis.text.x = element_text(angle = 45)) +  theme(legend.position='none')+  ylim(0, 3)

p_fam_body_2_4 = data %>% dplyr::filter(body.part == "Body") %>%
  dplyr::filter(Family %in% c("2","4")) %>%
  ggplot(aes(y=Ploidy, x=Stage_original, fill = Sex, lable = Stage_original)) + 
 geom_boxplot(outlier.shape = NA, coef=0 ) +  theme_bw() +  geom_jitter(width=0.1, size=2) +
  facet_wrap(~Family ) + ggtitle('Mite Ploidy per family') +
   theme(axis.text.x = element_text(angle = 45)) +  theme(legend.position='none')+  ylim(0, 3)

p_fam_body_5 = data %>% dplyr::filter(body.part == "Body") %>%
  dplyr::filter(Family == "5") %>%
  ggplot(aes(y=Ploidy, x=Stage_original, fill = Sex, lable = Stage_original)) + 
 geom_boxplot(outlier.shape = NA, coef=0 ) +  theme_bw() +  geom_jitter(width=0.1, size=2) +
  facet_wrap(~Family ) + ggtitle('Mite Ploidy per family') +
   theme(axis.text.x = element_text(angle = 45)) +  theme(legend.position='none')+  ylim(0, 3)

all developmental stages, males and females, whole body

all developmental stages, males and females, in different body parts


Looking at specific families

the Ploidy of the male offspring varied, depends on the family

for some families ( 1, 11, 27 and 3) the females Ploidy was higher then males.
while females looks diploid, males look haploid.


for other families ( 2 and 4) the females Ploidy was similar to that of the male offspring.


in one family (number 5) the male offspring Ploidy was mixed:
one males look diploid, and the other two look haploid.